from django.forms.models import model_to_dict
from BloodSpiderAPI.models import User


# 模型转字典
def utils_model_to_dict(model, *args, **kwargs):
    data = model_to_dict(model)
    
    # 转换uid为字符串
    if hasattr(model, 'uid'):
        data["uid"] = str(model.uid)
        # 移除id字段，使用uid
        if 'id' in data:
            del data['id']
    
    # 默认不显示密码
    if kwargs.get("show_password", False) is False:
        if 'password' in data:
            del data["password"]
    
    # 处理时间格式
    if kwargs.get("show_time", True):
        if hasattr(model, 'created_at'):
            data["created_at"] = model.created_at.strftime("%Y-%m-%d %H:%M:%S")
        if hasattr(model, 'updated_at'):
            data["updated_at"] = model.updated_at.strftime("%Y-%m-%d %H:%M:%S")
    
    return data